home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / RGASM.RAR / ASMCODE.EXE / CHAPT9 / COPYA.ASM < prev    next >
Encoding:
Assembly Source File  |  1993-05-10  |  16.9 KB  |  513 lines

  1. ;**********************************************************
  2. ;  Program CopyA ( Chapter 9 )
  3. ;
  4. ;  The program for fast files copying 
  5. ;
  6. ;  Author: A.I.Sopin, Voronezh University  1990 - 1993 
  7. ;
  8. ;  MS-DOS disk service  (INT 21h) is used
  9. ;
  10. ;  To exit program press the ESC key
  11. ;
  12. ;**********************************************************
  13. ;   MACRO  Definitions
  14. ;**********************************************************
  15. ; 1. Push common registers onto the stack:
  16. ;**********************************************************
  17. PUSHR    MACRO
  18.      push   ax
  19.      push   bx
  20.      push   cx
  21.      push   dx
  22.      push   bp
  23.      push   ds
  24.      push   es
  25.      push   si
  26.      push   di
  27.      ENDM
  28. ;**********************************************************
  29. ; 2. Pop common registers from the stack:
  30. ;**********************************************************
  31. POPR     MACRO
  32.      pop    di
  33.      pop    si
  34.      pop    es
  35.      pop    ds
  36.      pop    bp
  37.      pop    dx
  38.      pop    cx
  39.      pop    bx
  40.      pop    ax
  41.      ENDM
  42. ;********************************************************** 
  43. ; 3. Calling video library (DIAMS)
  44. ;********************************************************** 
  45. DIAM    MACRO   OP$,M$,N$,U$,V$
  46.     mov     TOFF,bx
  47.     mov     OP,OP$
  48.     mov     bx,M$
  49.     mov     M,bx
  50.     mov     bx,N$
  51.     mov     N,bx
  52.     mov     bx,U$
  53.     mov     U,bx
  54.     mov     bx,V$
  55.     mov     V,bx
  56.     lea     bx,PARM
  57.     push    bx
  58.     Call    far ptr DIAM00
  59.     mov     bx,TOFF
  60.     ENDM
  61. ;******************************************
  62. EXTRN    DIAM00 : FAR, DIAM01 : FAR, CLEAR : FAR, BINASC : FAR
  63. EXTRN    ADD32 : FAR, ERRCOD : FAR
  64. ;
  65. ;-----------------------------------------------------------------------
  66. STAC     SEGMENT STACK
  67.      DB      512 dup (?)
  68. STAC     ENDS
  69. ;-----------------------------------------------------------------------
  70. ;
  71. ;-----------------------------------------------------------------------
  72. DATA     SEGMENT  PARA PUBLIC 'DATA'
  73. TEXT0    db     ' Press ESC to terminate the program',0
  74. TEXT1    db     ' Input file name  =                                 ',0
  75. TEXT2    db     ' Output file name =                                 ',0
  76. TEXT3    db     ' File has been processed.  Time = hh/mm/ss/0.tt.'
  77.      db     '  Press any key',0
  78.      dw     20 dup ('  ')
  79. HH0      EQU    TEXT3+34
  80. MM0      EQU    TEXT3+37
  81. SS0      EQU    TEXT3+40
  82. TT0      EQU    TEXT3+45
  83. ;
  84. HRS      DB     0
  85. MINS     DB     0
  86. SECS     DB     0
  87. HSECS    DB     0
  88. CX2      dw     0
  89. DX2      dw     0
  90. BUFFER   db     12 dup (0)        ; buffer used for conversion: AX ---> ASCII
  91.      EVEN
  92. PARM     LABEL  WORD              ; parameters vector (its address is in
  93.                   ; the stack)
  94. OP       dw     0                 ; instruction code
  95. M        dw     0                 ; number of the first string
  96. N        dw     0                 ; number of the last string
  97. TOFF     dw     0                 ; text string offset
  98. L        dw     80                ; length of the text string
  99. U        dw     0                 ; string where the cursor is set (1---25)
  100. V        dw     0                 ; position where the cursor is set (1---80)
  101. Q        dw     0                 ; cursor row accepted 
  102. S        dw     0                 ; cursor column accepted 
  103. W        dw 2 dup (0)             ; interrupt key code
  104. IOFF     dw     0                 ; the address of the screen format vector
  105.                   ; (or 0)
  106. I        dw     80
  107.      db     0, 07h
  108. ;
  109. PATH1    DB     68 dup (0)        ;
  110. PATH2    DB     68 dup (0)        ;
  111. ERR0     DW     0                 ; error flag; error if not 0
  112. ERRTXT   DB     80 dup (0)        ; error messages buffer
  113. AERRTXT  DD     ERRTXT
  114. ATTN     DB     ' Attention!  - File is being processed. Please wait. ',0
  115. LATT     EQU    $-ATTN
  116. ;
  117. I1       DW     1
  118.      DB     0,07h
  119.      DW     LATT
  120.      DB     0,74h
  121.      DW     38
  122.      DB     0,07h
  123.      DW     0
  124. ;
  125. TERM     DB     0                 ;
  126. OP0      DB     0                 ;
  127. OP1      DB     0                 ;
  128. OP2      DB     0                 ;
  129. IO       DB     0                 ;  IO =0 - input file
  130. HANDLE0  DW     0                 ;
  131. HANDLE1  DW     0                 ;
  132. HANDLE2  DW     0                 ;
  133. ADRPAT   DW     0                 ;
  134. AMEM     LABEL  DWORD             ;  offset + segment address for LDS
  135. DX0      DW     0                 ;  offset to be loadedinto DX
  136. ADRMEM   DW     0                 ;  segment address of memory received
  137. Leng     dw     0
  138. Count1   dw     0                 ;  file length couner(the most significant
  139.                   ;  part)
  140. Count2   dw     0                 ;  file length counter(the least
  141.                   ;  significant part)
  142. CTXT     db     ' Number of bytes processed =             ',0
  143. ANUMB    DD     CTXT+29           ;  offset + segment address for LES
  144. DATA     ENDS
  145. ;-----------------------------------------------------------------------
  146. ;
  147. ;-----------------------------------------------------------------------
  148. _TEXT    SEGMENT PARA PUBLIC 'CODE'
  149.      ASSUME CS:_TEXT, DS:DATA, SS:STAC
  150. START:
  151.      mov    ax,DATA           ;  data segment basing
  152.      mov    ds,ax
  153.      mov    ax,STAC
  154.      mov    SS,ax             ;  internal program stack will be used
  155.      mov    SP,512            ;  stack size in bytes
  156.      mov    ADRMEM,0          ;  no memory has been allocated
  157. ;  Free memory above the program end
  158.      mov    bx,ZSEG           ;  program end address
  159.      mov    ax,es             ;  segment address of the program beginning
  160.      sub    bx,ax             ;  memory size needed
  161.      mov    ah,4Ah            ;  DOS function number
  162.      int    21h               ;  free memory
  163.      mov    ERR0,ax
  164.      jnc    Getmem            ;  Memory reallocated!
  165.      jmp    TYPERR
  166. ;  Allocate the maximum memory area for the buffer
  167. Getmem:  mov    ah,48h            ;  DOS function number
  168.      mov    bx,0FFFH          ;  request memory area of the maximum size
  169.      int    21h               ;
  170.      jnc    Norm              ;  jump if successful
  171.      int    21h               ;  BX = size of largest available block!
  172.      jnc    Norm              ;  jump if memory allocated
  173.      jmp    TYPERR            ;  otherwise output error message
  174.  
  175. Norm:    mov    ADRMEM,ax         ;  AX:0 -segment address of allocated
  176.                   ;  memory block
  177.      mov    cl,4              ;  Leng will be equal to the size
  178.      sal    bx,cl             ;  of allocated memory
  179.      mov    leng,bx           ;  in bytes
  180. ;  Clear screen and output string =2
  181. BEGIN:   mov    Count1,0          ;  clear file length
  182.      mov    Count2,0          ;  counters
  183.      mov    ch,1
  184.      mov    dh,24
  185.      Call   CLEAR             ;  screen clear
  186.      lea    bx,TEXT0          ;  message text address
  187.      DIAM   1,2,2,0,0         ;  prepare parameters for  DIAM00
  188.                   ;  and call it
  189. ;  Ask input and output files' names
  190.      mov    OP1,0
  191.      mov    OP2,0
  192.      mov    IO,0              ;  input file flag (read)
  193.      mov    M,3               ;
  194.      mov    U,3               ;
  195.      mov    N,-3              ;
  196.      mov    V,20              ;
  197.      lea    ax,PATH1          ;
  198.      mov    ADRPAT,ax         ;
  199.      mov    bx,offset TEXT1   ;
  200.      Call   GETFILE           ;
  201.      cmp    term,1            ;
  202.      jne    par1              ;
  203.      jmp    Close1            ;
  204. par1:    cmp    ERR0,0            ;
  205.      jz     movop1            ;
  206.      jmp    TYPERR            ;
  207. movop1:  mov    ax,HANDLE0        ;
  208.      mov    HANDLE1,ax        ;
  209.      mov    al,OP0            ;
  210.      mov    OP1,al            ;
  211. ;  Ask and input the output file name
  212. Input2:
  213.      mov    IO,1              ;  output file flag
  214.      mov    M,4               ;
  215.      mov    U,4               ;
  216.      mov    N,-4              ;
  217.      lea    ax,PATH2          ;
  218.      mov    ADRPAT,ax         ;
  219.      mov    bx,offset TEXT2   ;
  220.      Call   GETFILE           ;
  221.      cmp    TERM,1            ;  work terminated?
  222.      jne    par2              ;
  223.      jmp    Close1            ;
  224. par2:    cmp    ERR0,0            ;
  225.      jz     movop2            ;
  226.      jmp    TYPERR            ;
  227. movop2:  mov    ax,HANDLE0        ;
  228.      mov    HANDLE2,ax        ;
  229.      mov    al,OP0            ;
  230.      mov    OP2,al            ;
  231. ;  Output message about the file processing
  232.      lea    bx,I1
  233.      mov    IOFF,bx
  234.      lea    bx,ATTN
  235.      DIAM   1,5,-5,0,0        ;
  236. ;  Store the time value of file processing start
  237.      mov    ah,2CH            ;  get system time
  238.      int    21h
  239.      mov    HRS,ch            ;  hours
  240.      mov    MINS,cl           ;  minutes
  241.      mov    SECS,dh           ;  seconds
  242.      mov    HSECS,dl          ;  hundredths of seconds
  243. ;  Read the records of the input file (3072 bytes????)
  244. READ:    mov    bx,HANDLE1        ;
  245.      mov    cx,16384          ;
  246.      lds    dx,AMEM           ;  load: DX=0, DS is the segment address
  247.      mov    ah,3Fh            ;  read function code
  248.      int    21h               ;
  249.      mov    dx,DATA           ;  data segment address
  250.      mov    ds,dx             ;  restore the segment register
  251.      jnc    eof1              ;
  252.      jmp    TYPERR            ;
  253. eof1:    cmp    ax,0              ;  no bytes have been read?
  254.      jnz    WRITE
  255.      jmp    Close1            ;  yes, end of file
  256. ;  Move the record received into the output file
  257. WRITE:   mov    cx,ax             ;  CX:=the counter of bytes to be written
  258.      mov    dx,Count1         ;  the most significant part
  259.                   ;  of the byte counter
  260.      mov    ax,Count2         ;  the least significant part
  261.                   ;  of the byte counter
  262.      add    ax,cx             ;  double precision
  263.      adc    dx,0              ;  add operation
  264.      mov    Count1,dx         ;
  265.      mov    Count2,ax         ;
  266.      mov    bx,HANDLE2        ;
  267.      lds    dx,AMEM           ;  load: DX=0, DS is the segment address
  268.      mov    ah,40h            ;  write function code
  269.      int    21h               ;
  270.      mov    dx,DATA           ;  data segment address
  271.      mov    ds,dx             ;  restore the segment register
  272.      jnc    short READ        ;  jump to read the next record
  273.      mov    ERR0,ax           ;  store the result into error flag
  274. ;
  275. ;-----------------------------------------------------------------------
  276. ;  Output file processing error message (error code is in the AX register)
  277. TYPERR:  mov    cx,40
  278.      les    DI,AERRTXT        ;
  279.      cld                      ;
  280.      xor    ax,ax             ;
  281.      rep    stosw
  282.      mov    ax,ERR0
  283.      les    DI,AERRTXT        ;
  284.      Call   ERRCOD            ;
  285.      cld                      ;
  286.      rep    movsb             ;  accept text for error message
  287. ;  Error message output
  288.      mov    ax,DATA
  289.      mov    ds,ax             ;  DX:=data segment address
  290.      lea    bx,ERRTXT         ;
  291.      mov    IOFF,0            ;
  292.      DIAM   3,4,-4,0,0        ;
  293. ;  Close input and output files
  294. Close1:  mov    ERR0,0
  295.      cmp    OP1,0             ;  is the input file opened?
  296.      jz     Close2            ;  No!
  297.      mov    ah,3Eh            ;
  298.      mov    bx,HANDLE1        ;  input file pointer
  299.      int    21h               ;
  300.      mov    ERR0,ax
  301.      jnc    Close2            ;  jump if threr are no errors
  302.      jmp    TYPERR            ;  output close error message
  303. Close2:  mov    ERR0,0
  304.      cmp    OP2,0             ;  is the input file opened?
  305.      jnz    cls2              ;  yes, close the file
  306.      cmp    term,1            ;  work terminated?
  307.      jne    jmpbeg
  308.      jmp    EOJ               ;
  309. jmpbeg:  jmp    BEGIN             ;  go to input request and process the next file
  310. cls2:    mov    ah,3Eh            ;  close function code
  311.      mov    bx,HANDLE2        ;  output file pointer
  312.      int    21h               ;
  313.      mov    ERR0,ax
  314.      jnc    gobeg             ;
  315.      jmp    TYPERR            ;  output close error message
  316. gobeg:   cmp    term,1            ;  work terminated?
  317.      jne    Alltime
  318.      jmp    EOJ               ;
  319. ;
  320. ;-----------------------------------------------------------------------
  321. ;  Calculating the total time of file processing
  322. Alltime:
  323.      mov    ah,2CH            ;  get system time
  324.      int    21h
  325.      sub    dl,HSECS
  326.      jnc    sub_secs
  327.      add    dl,100
  328.      dec    dh
  329. sub_secs:
  330.      sub    dh,SECS
  331.      jnc    sub_mins
  332.      add    dh,60
  333.      dec    cl
  334. sub_mins:
  335.      sub    cl,MINS
  336.      jnc    sub_hrs
  337.      add    cl,60
  338.      dec    ch
  339. sub_hrs:
  340.      sub    ch,HRS
  341.      jnc    HHMMSSTT
  342.      add    ch,24
  343. ;  Calculating the number of hours/minutes (CH/CL)
  344. HHMMSSTT:
  345.      xor    ax,ax
  346.      mov    al,ch
  347.      Call   DIGIT2
  348.      mov    word ptr HH0,bx
  349.      xor    ax,ax
  350.      mov    al,cl
  351.      Call   DIGIT2
  352.      mov    word ptr MM0,bx
  353. ;  Calculating the number of seconds/hundredth of seconds (DH/DL)
  354.      xor    ax,ax
  355.      mov    al,dh
  356.      Call   DIGIT2
  357.      mov    word ptr SS0,bx
  358.      xor    ax,ax
  359.      mov    al,dl
  360.      Call   DIGIT2
  361.      mov    word ptr TT0,bx
  362. ;  Output file processing time (string = 6)
  363.      mov    IOFF,0            ;
  364.      lea    bx,TEXT3
  365.      DIAM   1,6,6,0,0         ;
  366. ;  Convert the read bytes counter into ASCII:
  367.      lea    bx,BUFFER         ;  work buffer address (12 bytes)
  368.      xor    dx,dx             ;  the most significant part of the number
  369.      mov    dx,Count1
  370.      mov    ax,Count2
  371.      Call   BINASC            ;  conversion: BIN ---> ASCII
  372.      push   cx
  373.      les    di,ANUMB          ;  ES:DI:= target address
  374.      cld                      ;
  375.      mov    cx,6
  376.      mov    ax,'  '
  377.      rep    stosw
  378.      mov    si,bx
  379.      les    di,ANUMB
  380.      pop    cx
  381.      rep    movsb             ;
  382. ;  Output the number of bytes processed (string =7)
  383.      mov    IOFF,0
  384.      lea    bx,CTXT
  385.      DIAM   2,7,-7,7,30
  386.      jmp    BEGIN             ;  go to input request and process
  387.                   ;  the next file
  388. ;-------------------------------------------------------------------
  389. ;  Terminate the program:
  390. EOJ:     Call   FREEMAIN          ;  free the memory buffer allocate
  391.      mov    ax,4C00h          ;  normal program termination
  392.      int    21h               ;  Return to  MS-DOS
  393.  
  394. ;***********************************************************
  395. ;
  396. ;  Convert a two-digit number from
  397. ;
  398. ;  binary code into ASCII code
  399. ;
  400. ;
  401. ;***********************************************************
  402. DIGIT2   PROC   NEAR
  403.      push   cx
  404.      push   dx
  405.      xor    dx,dx
  406.      lea    bx,BUFFER
  407.      Call   BINASC
  408.      lea    bx,BUFFER+10
  409.      cmp    byte ptr [bx],20h
  410.      jne    Timret
  411.      mov    byte ptr [bx],'0'
  412. Timret:
  413.      mov    bx,word ptr [bx]
  414.      pop    dx
  415.      pop    cx
  416.      RETN
  417. DIGIT2   ENDP
  418. ;
  419. ;***********************************************************
  420. ;
  421. ;  Free the memory buffer allocated
  422. ;
  423. ;***********************************************************
  424. FREEMAIN PROC   NEAR
  425.      mov    ax,ADRMEM         ;  memory segment address
  426.      cmp    ax,0              ;  memory allocated ?
  427.      jnz    Fremem            ;  yes !!!
  428.      jmp    short freret      ;
  429. Fremem:  mov    ERR0,0            ;  clear error code
  430.      mov    es,ax             ;  memory segment address
  431.      mov    ah,49h            ;  free memory function code
  432.      int    21h               ;  free memory
  433.      jnc    Freret            ;
  434.      mov    ERR0,ax           ;  free memory error code
  435.      jmp    TYPERR            ;  error message output
  436. Freret:  RETN
  437. FREEMAIN ENDP
  438. ;
  439. ;**********************************************************
  440. ;
  441. ;  Input the requested file name and open the file
  442. ;
  443. ;  bx     - text string address
  444. ;
  445. ;  ADRPAT - address of the string that contains the requested file path
  446. ;
  447. ;
  448. ;**********************************************************
  449. GETFILE  PROC   NEAR
  450.      PUSHR
  451.      mov    ERR0,0            ; no errors detected yet
  452.      mov    IOFF,0            ;
  453.      DIAM   3,M,N,U,V         ; output string that requests file name
  454.      mov    ax,W
  455.      mov    term,0            ;
  456.      cmp    al,1Bh            ; ESC ?
  457.      jne    Movpath
  458.      mov    term,1            ; end of work flag
  459.      jmp    Ret01
  460. ;  Move the requested file name to the path string
  461. Movpath: add    bx,V              ;  the address of symbol next to the cursor
  462.      mov    di,ADRPAT
  463.      mov    ax,ds
  464.      mov    es,ax
  465. M1:      mov    cl,[bx-1]         ;  move the name character
  466.      mov    es:[di],cl        ;
  467.      cmp    cl,0
  468.      jz     Open2
  469.      cmp    cl,' '
  470.      je     Open2
  471.      cmp    cl,0Dh            ;  carriage return ?
  472.      je     Open2             ;
  473.      inc    di                ;
  474.      inc    bx                ;
  475.      jmp    short M1          ;  to the cycle beginning
  476. ;  Open the file requested and analyze the result
  477. Open2:   mov    OP0,0             ;
  478.      mov    ah,3Dh            ;
  479.      mov    al,2              ; al:=access mode
  480.      mov    dx,ADRPAT         ; DX:=file name (and path) address
  481.      int    21h               ;
  482.      jc     ERROR             ;
  483.      mov    HANDLE0,ax        ; store the file handle
  484.      mov    OP0,1             ; open file flag
  485.      jmp    Ret00
  486. ;  Open file errors processing:
  487. ERROR:   mov    ERR0,ax           ;  error code
  488.      cmp    ax,2              ;  file not found
  489.      jne    Ret01             ;  not found, output error message
  490. cmpio:   cmp    IO,0              ;  input file?
  491.      jz     Ret01             ;  yes, output message
  492. ;  Create and open output file:
  493. Create:  mov    ERR0,0            ;  clear error flag
  494.      lea    dx,PATH2          ;  DX:=output file path offset
  495.      mov    ah,3Ch            ;
  496.      mov    cx,0              ;  CX:=file attributes
  497.      int    21h               ;
  498.      mov    HANDLE0,ax        ;  access word (file handle)
  499.      jnc    ret00             ;
  500.      mov    ERR0,ax           ;  error code
  501.      jmp    short Ret01       ;
  502. ;  POP registers and exit
  503. Ret00:   mov    OP0,1             ;
  504. Ret01:   POPR
  505.      RETN
  506. GETFILE  ENDP
  507. _TEXT    ENDS
  508. ;
  509. ;-----------------------------------------------------------------------
  510. ZSEG     SEGMENT  'ZERO'
  511. ZSEG     ENDS
  512.      END    START
  513.